﻿using MainInterface;
using MainService;
using MainZNQD;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace MainProGrid
{
    /// <summary>
    /// 自定义属性实体类
    /// </summary>
    public class CustomProperty
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Component">依赖组件</param>
        /// <param name="PropertyType">属性类型</param>
        /// <param name="ChinaName">属性中文名称</param>
        /// <param name="Name">属性名</param>
        /// <param name="GroupName">属性分组</param>
        /// <param name="Description">属性描述</param>
        public CustomProperty(IProperType Component, Type PropertyType, string ChinaName, string Name, string GroupName, string Description) 
        {
            this.Component = Component;
            this.PropertyType = PropertyType;
            this.ChinaName = ChinaName;
            this.Name = Name;
            this.GroupName = GroupName;
            this.Description = Description;
            AddUrlConvert();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Component">依赖组件</param>
        /// <param name="PropertyType">属性类型</param>
        /// <param name="ChinaName">属性中文名称</param>
        /// <param name="Name">属性名</param>
        /// <param name="GroupName">属性分组</param>
        /// <param name="Description">属性描述</param>
        public CustomProperty(IProperType Component, Type PropertyType, string ChinaName, string Name, string GroupName, string Description,bool ReadyOnly)
        {
            this.Component = Component;
            this.PropertyType = PropertyType;
            this.ChinaName = ChinaName;
            this.Name = Name;
            this.GroupName = GroupName;
            this.Description = Description;
            this.ReadyOnly = ReadyOnly;
            AddUrlConvert();
        }

        private void AddUrlConvert()
        {
            if (this.Name.ToLower() == "url"&&this.PropertyType==typeof(string) )
            {
                if (MyServer.CurrentUrls == null) 
                {
                    MyServer.CurrentUrls = new List<string>();
                    MyServer.CurrentUrlDescriptions = new List<string>();
                    foreach (MainZNQD.Action action in MyServer.OldCurrentSolution.Actions)
                    {
                        MyServer.CurrentUrls.Add(action.Url);
                        MyServer.CurrentUrlDescriptions.Add(action.Description);
                    }
                }
                UrlEidtor refereditor = new UrlEidtor();
                refereditor.Descriptions = MyServer.CurrentUrlDescriptions;
                refereditor.Urls = MyServer.CurrentUrls;
                this.Editor = refereditor;
            }
            else if (this.Name == "Reference" && this.PropertyType == typeof(string)) 
            {
                ReferenceEditor reditor = new ReferenceEditor();
                this.Editor = reditor;
            }
        }
        #region 
        public bool ReadyOnly = false;
        public TypeConverter Converter { get; set; }
        public virtual object Editor { get; set; }
        public IProperType Component { get; set; }
        public Type PropertyType { get; set; }
        public string ChinaName { get; set; }
        public string Name { get; set; }
        public string GroupName { get; set; }
        public string Description { get; set; }
        public object Value {
            get {
               return Component.GetValue(this.Name);
            }
            set {
                Component.SetValue(this.Name, value);
            }
        }
        #endregion
    }
}
